<template>
  <div class="bgc-container">
    <div class="login-container">
      <div class="bgc-black">
        <div class="right-form">
          <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
            <div class="title">欢迎登录</div>
            <div class="form-sty">
              <el-form-item prop="username">
                <el-input
                  ref="username"
                  v-model.trim="loginForm.username"
                  placeholder="填写用户名"
                  name="username"
                  type="text"
                  tabindex="1"
                  auto-complete="on"
                />
                <span class="svg-container">
                  <svg-icon icon-class="user1" class="left-login" />
                </span>
                <span @click="resetUser">
                  <svg-icon icon-class="del" class="right-svg" />
                </span>
              </el-form-item>
              <el-form-item prop="password">
                <el-input
                  :key="passwordType"
                  ref="password"
                  v-model.trim="loginForm.password"
                  :type="passwordType"
                  placeholder="密码"
                  name="Password"
                  tabindex="2"
                  auto-complete="on"
                  @keyup.enter.native="handleLogin"
                />
                <span class="svg-container">
                  <svg-icon icon-class="password1" class="left-login" />
                </span>
                <span @click="showPwd">
                  <svg-icon :icon-class="passwordType === 'password' ? 'show' : 'open'" class="right-svg" />
                </span>
              </el-form-item>
              <el-button :loading="loading" class="button-style" @click.native.prevent="handleLogin">登录</el-button>
            </div>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name: 'Login',
  data() {
    return {
      paramValue: '',
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{ required: true, message: '请输入当前账号', trigger: 'blur' }],
        password: [{ required: true, message: '请输入当前密码', trigger: 'blur' }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    resetUser() {
      this.loginForm.username = ''
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('permission/clearPermission_routes').then(() => {
            this.$store.dispatch('user/login', this.loginForm).then(() => {
              this.$router.push({ path: '/' })
              this.loading = false
            }).catch(() => {
              this.$message.error('输入的用户名或密码不正确！')
              this.loading = false
            })
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#2D3339;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: black;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    line-height: 47px;
    .el-input__inner{
      width:280px !important;
    }
    input {
     padding: 0 35px 0 25px;
      &:-webkit-autofill {
        box-shadow: 0 0 0 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }
  .el-input--mini{
    font-size: 15px;
  }
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#2E6BC8;
$text_gray:#515668;
.bgc-container{
  overflow: hidden;
  background: #091123;
  position: relative;
}
.login-container{
  width:1920px;
  height:calc(100vh);
  overflow: hidden;
  background-repeat: no-repeat;
  .right-form{
    .title{
      font-size: 22px;
      color: #696969;
      margin: 40px auto;
      text-align: center;
    }
    .login-form{
      width: 380px;
    }
    .button-style{
      width: 280px;
      height: 48px;
      margin-top: 30px;
      background: $light_gray;
      color: #fff;
      font-size: 15px;
      border: none;
    }
    .svg-container {
      display: inline-block;
    }
    .svg-icon{
      width: 16px;
      height: 16px;
    }
    .left-login{
      position: absolute;
      left: 5px;
      top: 15px;
    }
    .right-svg{
      position: absolute;
      right: 10px;
      top: 15px;
    }
    .form-sty{
      margin: 0 50px;
    }
  }
}
.bgc-black{
  position: absolute;
  width: 380px;
  height: 400px;
  background: #fff;
  border-radius: 8px;
  bottom: calc(50% - 190px);
  right: calc(50% - 200px);
}
img{
  width: 100%;
}
.box-sty{
  .btm-sty{
    margin-bottom: 16px;
  }
  img {
    width: 12px;
    height: 12px;
    margin-right: 10px;
  }
}

</style>
